<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('农事记录')"/>
    <style>
        .fixed-table-toolbar .bs-bars {
            width: 100%;
        }
    </style>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">

        <div class="col-sm-12 search-collapse">
            <div class="btn-group " style="margin: 0px auto;display: table;" role="group">
                <button type="button" class="top-link btn btn-sm btn-primary">当天农事</button>
                <button type="button" class="top-link btn-sm btn-white">在执行</button>
                <button type="button" class="top-link btn-sm btn-white">待执行</button>
            </div>

        </div>

        <div class="btn-group-sm " id="toolbar1" role="group">
            <h3 class="inline">工作内容：</h3>
        </div>
        <div class="col-sm-12 select-table table-striped ">
            <table id="bootstrap-table-1" data-mobile-responsive="true"></table>
        </div>

        <!--计划详情-->
        <div class="col-sm-12 search-collapse">
            <h3>计划详情：</h3>
            <div class="thumbnail col-sm-3" style="width: 140px; height: 140px;">
                <img class="standard-img" src="/img/profile.jpg">
            </div>
            <div class="col-sm-9 detail-box">
                <div class="ibox-title">
                    <h4 class="stageName">生产阶段：</h4>
                </div>
                <div class="ibox-title">
                    <h4 class="inline">计划编号：</h4><span class="kv-v planCode">--</span>
                    <h4 class="inline">作物名称：</h4><span class="kv-v cropNameCn">--</span>
                    <h4 class="inline">作物品种：</h4><span class="kv-v cropVarietiesCn">--</span>
                    <h4 class="inline">计划时间：</h4><span class="kv-v planTime">--</span>
                </div>
                <div class="btn-group ibox-title" role="group">
                    <button type="button" class="work-logs middle-link btn-sm btn-success">农事记录</button>
                    <button type="button" class="zhibao-logs middle-link btn-sm btn-success">植保请求记录</button>
                    <button type="button" class="zhibao-add middle-link btn-sm btn-primary">植保请求</button>
                    <button type="button" class="nongzi-add middle-link btn-sm btn-primary">新增农资</button>
                    <button type="button" class="work-add middle-link btn-sm btn-primary">新增农事</button>

                </div>
            </div>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <h3 class="inline">农事作业完成表：</h3>

        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table" data-mobile-responsive="true"></table>
        </div>
    </div>
    <div class="bottom-btns"></div>
</div>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('system:ncWorkLog:edit')}]];
    var removeFlag = [[${@permission.hasPermi('system:ncWorkLog:remove')}]];
    var prefix = ctx + "system/ncWorkLog";

    var planType = [[${type}]];
    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "温室管理",
            id:"bootstrap-table",
            sidePagination:"client",
            pageSize: 8,
            showSearch: false,
            showRefresh: false,
            showColumns: false,
            showToggle: false,
            showExport: false,
            queryParams: {params: {planType: planType, beginTime: dateFtt(new Date())}},//只显示当天的
            columns: [
                {
                    field: 'zaipeiId',
                    title: '栽培记录主键',
                    visible: false
                }, {
                    title: '序号',
                    formatter: function (value, row, index) {
                        return $.table.serialNumber(index);
                    }
                },
                {
                    field: 'workName',
                    title: '农事名称',
                    sortable: true
                },
                {
                    field: 'workType',
                    title: '农事类型',
                    formatter: function (value, row, index) {
                        if (value == 1) {
                            return "栽培";
                        } else if (value == 2) {
                            return "农资";
                        } else {
                            return '--';
                        }
                    }
                },
                {
                    field: 'source',
                    title: '来源',
                    formatter: function (value, row, index) {
                        if (value == 1) {
                            return "标准";
                        } else if (value == 2) {
                            return "新增";
                        } else {
                            return '--';
                        }
                    }
                },
                {
                    title: '作业内容',
                    sortable: false,
                    formatter: function (value, row, index) {
                        var str = '';
                        if (!row.plan) {
                            row.plan = {};
                        }
                        // str += row.plan.planCode + " - ";
                        str += row.plan.cropNameCn + " - ";
                        str += row.plan.cropVarietiesCn + " - ";
                        // str += dateFtt(row.plan.planTime) + " - ";
                        str += !row.workName ? '' : row.workName + " - ";
                        str += !row.plan.peng ? '' : row.plan.peng;
                        if (row.workType == '2') {
                            var nz = row.inputCn ? row.inputCn : '**';
                            var count = row.inputCount ? row.inputCount : '**';
                            var unit = $.table.selectDictLabel(normal_unit, row.inputUnit);
                            unit = unit ? unit : '**'
                            str += '（' + nz + '：' + count + unit + ')'
                        }
                        return str;
                    }
                },

                {
                    field: 'workDetail',
                    title: '详情描述',
                    formatter: function (value, row, index) {

                        return $.table.tooltip(getText(value), 20);
                    }
                },
                {
                    field: 'createTime',
                    title: '完成时间',
                    sortable: true,
                    formatter: function (value, row, index) {
                        return dateFtt(value);
                    }
                },
                {
                    field: 'remark',
                    title: '备注',
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                }
                ]
        };
        $.table.init(options);

        initTodayWorks();


        $('.top-link').click(function () {
            var index = $(this).index();
            switch (index) {
                case 0:
                    window.location.reload();
                    return;
                case 1:
                    window.location.href = ctx + 'system/ncWorkLog/ncWorkLogWorking?type=' + planType;
                    return;
                case 2:
                    window.location.href = ctx + 'system/ncWorkLog/ncWorkLogPreWorking?type=' + planType;
                    return;

            }
        })
    });
    var fillPlanDetail = function (row, $element) {
        //阶段
        var str = '阶段：';

        if (row.stages) {
            $(row.stages).each(function (i, e) {
                /*1:完成 2：执行中 3：未进入*/
                var color = '';
                if (i >= topColor.length) {
                    color = topColor[i - topColor.length];
                } else {
                    color = topColor[i];
                }
                if (e.stageStatus == 1) {
                    str += '<span class="finished" style="background-color: ' + color + '">' + e.stageName + '</span>'
                } else if (e.stageStatus == 2) {
                    str += '<span class="working" style="background-color: ' + color + '">' + e.stageName + '<i class="fa fa-circle text-danger" style="margin-left: 2px"></i></span>'
                } else if (e.stageStatus == 3) {
                    str += '<span class="pre-working" style="background-color: ' + color + '">' + e.stageName + '</span>'
                }

            });
        }
        if (!row.plan) {
            row.plan = {};
        }
        $('.stageName').html(str);
        $('.planCode').html(row.plan.planCode);
        $('.cropNameCn').html(row.plan.cropNameCn);
        $('.cropVarietiesCn').html(row.plan.cropVarietiesCn);
        $('.planTime').html(dateFtt(row.plan.planTime));

        if (row.standard && row.standard.pic) {
            $('.standard-img').attr('src', row.standard.pic)
        } else {
            $('.standard-img').attr('src', '/img/profile.jpg')
        }

        $('.detail-box').attr('planId', row.plan.planId)
        $('.detail-box').attr('stageId', row.stageMirrorId)
    }
    var normal_unit = [[${@dict.getType('normal_unit')}]];

    /*初始化当天工作*/
    function initTodayWorks() {
        var prefix = ctx + "system/ncWork";

        var options2 = {
            url: prefix + "/listTodays",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "标准农事",
            id: 'bootstrap-table-1',
            pageSize: 8,
            sidePagination: "client",
            toolbar: 'toolbar1',
            showSearch: false,
            showRefresh: false,
            showColumns: false,
            showToggle: false,
            showExport: false,
            queryParams: {params: {planType: planType}},
            onClickRow: fillPlanDetail,
            onLoadSuccess: function (data) {
                if (data.length > 0) {
                    fillPlanDetail(data[0]);
                }
            },
            columns: [
                {
                    field: 'workId',
                    title: '农事id',
                    visible: false
                }, {
                    title: '序号',
                    formatter: function (value, row, index) {

                        return index + 1;

                        // return $.table.serialNumber(index);
                    }
                }, {
                    title: '计划编号',
                    formatter: function (value, row, index) {
                        return row.plan.planCode;
                    }
                },
                {
                    field: 'workName',
                    title: '农事名称',
                    sortable: true
                },
                {
                    title: '作业内容',
                    sortable: false,
                    formatter: function (value, row, index) {
                        var str = '';
                        if (!row.plan) {
                            row.plan = {};
                        }
                        // str += row.plan.planCode + " - ";
                        str += row.plan.cropNameCn + " - ";
                        str += row.plan.cropVarietiesCn + " - ";
                        // str += dateFtt(row.plan.planTime) + " - ";
                        str += row.workName + " - ";
                        // str += row.plan.peng;
                        str += !row.plan.peng ? '' : row.plan.peng;

                        if (row.workType == '2') {
                            var nz = row.inputCn ? '**' : row.inputCn
                            var count = row.inputCount ? '**' : row.inputCount
                            var unit = $.table.selectDictLabel(normal_unit, row.inputUnit);
                            unit = unit ? unit : '**'
                            str += '（' + nz + '：' + count + unit + '/亩)'
                        }
                        return str;
                    }
                },
                {
                    field: 'workType',
                    title: '农事类型',
                    formatter: function (value, row, index) {
                        if (value == 1) {
                            return '<span class="badge badge-primary">栽培</span>'
                        } else if (value == 2) {
                            return '<span class="badge badge-success">农资</span>'
                        } else {
                            return '--';
                        }
                    }
                },
                {
                    field: 'type',
                    title: '分类',
                    formatter: function (value, row, index) {
                        if (value == 1) {
                            return '<span class="badge badge-primary">农事</span>'
                        } else if (value == 2) {
                            return '<span class="badge badge-success">阶段</span>'

                        } else {
                            return '--';
                        }
                    }
                },
                {
                    field: 'workDate',
                    title: '工作日期',
                    formatter: function (value, row, index) {
                        return dateFtt(value);
                    }

                }, {
                    field: 'remark',
                    title: '备注',
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        if (row.type == 1) {
                            if (row.workType == '1') {
                                actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="confirmWork(\'' + row.workMirrorId + '\')"><i class="fa fa-edit"></i>确认</a> ');
                            } else if (row.workType == '2') {
                                actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="confirmWork2(\'' + row.workMirrorId + '\')"><i class="fa fa-edit"></i>确认</a> ');
                            }
                        } else if (row.type == 2) {
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="confirmStage(\'' + row.stageMirrorId + '\')"><i class="fa fa-edit"></i>确认</a> ');
                            actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="delayWork(\'' + row.stageMirrorId + '\')"><i class="fa fa-remove"></i>延迟</a>');
                        }
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options2);

        var btns = [{
                name: '采摘任务',
                url: ctx + 'system/ncWorkLog/harvestWork',
                id: 'caizhai-work'
            }, {
                name: '消毒管理',
                url: ctx + 'system/ncXiaoduLogs',
                id: 'xiaodu-manager'
            }, {
                name: '植保单',
                url: ctx + 'system/ncOrder?orderType=9',
                id: 'zhibao-bill'
            },
                {
                    name: '所有计划',
                    url: ctx + 'system/' + (planType == 1 ? 'ncPlan/allPlan' : 'ncPlan2/allPlan2'),
                    id: 'all-plan'
                }, {
                    name: '智能控制',
                    url: ctx + 'system/iot/ctrl',
                    id: 'auto-ctrl'
                }, {
                    name: '发芽率',
                    url: ctx + 'system/ncSeedRate',
                    id: 'seed-rate'
                }, {
                    name: '出库登记',
                    url: ctx + 'system/ncOutPut',
                    id: 'out-put'
                },
                {
                    name: '工作记录',
                    url:ctx + 'system/ncNormalWorks?workType=' + (planType == 1 ? 14 : 7),
                    id:'normal-work'
                }
            ]
        ;

        if (planType == 1) {
            btns[4].hidden = 1;
            btns[0].hidden = 1;
        }
        if (planType == 2) {
            btns[5].hidden = 1;
            btns[6].hidden = 1;
        }

        addBtns(btns);
        //按钮事件
        $('.work-logs').click(function () {
            var planId = $('.detail-box').attr('planId');
            if (valid(planId)) {
                toAllLogs(planId);
            }
        });
        $('.zhibao-logs').click(function () {
            var planId = $('.detail-box').attr('planId');
            if (valid(planId)) {
                var url = ctx + 'system/ncZhibao/ncZhibaoLogs?planId=' + planId;
                $.modal.openTab('请求记录', url);
            }
        });

        $('.zhibao-add').click(function () {
            var planId = $('.detail-box').attr('planId');
            var stageId = $('.detail-box').attr('stageId');
            if (valid(planId)) {
                var url = ctx + 'system/ncZhibao/addRequest?planId=' + planId + "&stageId=" + stageId;
                $.modal.open('植保请求', url);
            }
        });
        $('.nongzi-add').click(function () {
            var planId = $('.detail-box').attr('planId');
            var stageId = $('.detail-box').attr('stageId');
            if (valid(planId)) {
                var url = ctx + 'system/ncWorkLog/addNongZi?planId=' + planId + "&stageId=" + stageId;
                $.modal.open('新增农资', url);
            }
        });
        $('.work-add').click(function () {
            var planId = $('.detail-box').attr('planId');
            var stageId = $('.detail-box').attr('stageId');
            if (valid(planId)) {
                var url = ctx + 'system/ncWorkLog/addNongShi?planId=' + planId + "&stageId=" + stageId;
                $.modal.open('新增农事', url);
            }
        });
        $('.auto-ctrl').click(function () {

            var url = baseWlwUrl + '/product.html';
            $.modal.openTab('智能控制', url);
        });

    }

    function valid(planId) {
        if (!planId) {
            $.modal.msgWarning("未发现数据!")
            return false;
        } else {
            return true;
        }
    }


    /*确认工作*/
    function confirmWork(workId) {
        var url = prefix + '/confirmWork/' + workId;
        $.modal.open('确认工作', url);
    }

    /*确认农资*/
    function confirmWork2(workId) {
        var url = prefix + '/confirmWork2/' + workId;
        $.modal.open('确认工作', url);
    }

    /*确认阶段*/
    function confirmStage(stageMirrorId) {
        var url = prefix + '/confirmStage/' + stageMirrorId;
        $.modal.open('确认阶段', url);
    }

    /*延迟阶段*/
    function delayWork(stageMirrorId) {
        var url = prefix + '/delayStage/' + stageMirrorId;
        $.modal.open('延迟阶段', url);
    }

    /*所有农事记录*/
    function toAllLogs(planId) {
        var url = prefix + "/allWorkLogs"
        if (planId) {
            url = prefix + "/allWorkLogs?planId=" + planId
        }

        $.modal.openTab('农事记录', url);
    }

    function reload() {
        $('#bootstrap-table').bootstrapTable('refresh', {
            silent: true
        });
        $('#bootstrap-table-1').bootstrapTable('refresh', {
            silent: true
        });
    }

    if (planType == 1) {
        setTitle("育苗管理-当天农事", false)
    } else {
        setTitle("温室管理-当天农事", false)
    }


</script>
</body>
</html>